package servlet.Article;

import DAO.ArticleDAO;
import Model.Article;
import Model.User;
import appexception.AppException;
import service.BaseService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @program: wsn_blog
 * @description：新增文章操作
 * @author:王松年
 * @creat: 2021-07-12 20:17:56
 **/
@WebServlet("/add")
public class AddArticleServlet extends BaseService{

    @Override
    protected void query(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        Article article = objectMapper.readValue(req.getInputStream(), Article.class);
            if(article.getTitle()==null||article.getTitle().length()==0){
                throw new AppException("ADD000","请输入文章标题");
            }
            HttpSession session = req.getSession(false);
            User user = (User) session.getAttribute("user");
            article.setUser_id(user.getId());
            int i = ArticleDAO.insert(article);
            if (i!=1){
                throw new AppException("ADD001","插入文章失败");
            }
    }
}
